package controller;

import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import action.ActionMain;
import utilitaire.ListContact;
import vues.HomeWindow;
import vues.MainWindow;

/**
 * 
 * Class Controller du tableau de contact
 * listContactTmp		Objet ListContact
 * listContact			Objet ListContact
 * homeWindow			Objet HomeWindow
 * homeView				Objet MainWindow
 * listGrp				Objet ArrayList<String>
 * 
 * @author Paul BEZARD-FALGAS
 *
 */
public class ControllerListener implements ListSelectionListener {
		private ListContact listContactTmp;
		private ListContact listContact;
		private HomeWindow homeWindow;
		private MainWindow homeView;
		private ArrayList<String> listGrp;
		
    	public ControllerListener(MainWindow mainWindow) {
    		//this.homeView = mainWindow;
    		this.homeView = mainWindow;
    		this.listContactTmp = null;
    		this.homeWindow = this.homeView.getHomeWindow();
    		this.listContact = null;
    		this.listGrp = this.homeView.getListGrp();
		}

    		
    	public void valueChanged(ListSelectionEvent e) {
    		this.homeWindow.getContentPane().removeAll();
    		this.homeWindow.getContentPane().validate();
    		this.homeWindow.getContentPane().repaint();
    		
    		this.listContact = this.homeWindow.getList();
    		
    		listContactTmp = new ListContact();

    		System.out.println(e.getValueIsAdjusting() + " [" + e.getFirstIndex() + " " + e.getLastIndex() + "].");
    		if (e.getValueIsAdjusting()){

        		if(homeView.getJlist().getSelectedValue().toString().equals("All"))
        		{
        			listContactTmp=listContact;

        		}else {
	    	    	for(int i=0; i<listContact.size();i++ ){
	    		    	if(homeView.getJlist().getSelectedValue().toString().equals(listContact.get(i).getGroup())){
	    		    		listContactTmp.add(listContact.get(i));
	    		    	}
	    	    	}
        		}

	    		
	    		System.out.print("Je suis dans listener");
	    		
	    		
	    		new ActionMain(homeWindow, listContactTmp, listGrp);


	    		System.out.print("Je suis dans Listener AFter");
    		}
    	}
}
